!(function($){
    /**
     * InsuRebel is Object.
     * All global function and variable must belong to this object
     */
    var InsuRebel = function() {
        var context = this;
        this.iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false;
        this.setPositionBackTop = function() {
            if ($(window).scrollTop() > 200) {
                var scroll_bottom = $(document).height() - $(window).scrollTop() - $(window).height();
                if (scroll_bottom < 200) {
                    var extra = 150;
                    if (context.iOS) {
                        extra = 235;
                    }
                    $('#back-top').css('bottom', 30 + extra - scroll_bottom + 'px');
                } else {
                    $('#back-top').css('bottom', '30px');
                }
                $('#back-top').fadeIn();
            } else {
                $('#back-top').fadeOut();
            }
        };
        
    };

    $(document).ready(function() {
        //Initialize global InsuRebel object
        window.InsuRebel = new InsuRebel();
        
        // Remove alert after x time
        setTimeout(function() {
            $.utils.clearFlash();
        }, timeOutNumber);
        
        /**
         * Handle back top for load more
         */
        // hide #back-top first
        $(".back-top").hide();
        $(window).scroll(function () {
            window.InsuRebel.setPositionBackTop();
        });
        // scroll body to 0px on click
        $('#back-top a').click(function () {
            $('body,html').animate({
                    scrollTop: 0
            }, 800);
            return false;
        });
        
        // Handle confirm button
        $('body').on('click', '.link-confirm', function(e) {
            e.preventDefault();
            var that = this;
            var message = typeof $(this).data('confirm') != 'undefined' ? $(this).data('confirm') : "Message confirm here.";
            var action1 = typeof $(this).data('action1') != 'undefined' ? $(this).data('action1') : "<i class='fa fa-times'></i> Cancel";
            var action2 = typeof $(this).data('action2') != 'undefined' ? $(this).data('action2') : "<i class='fa fa-check'></i> Ok";
            bootbox.confirm({
                message: message,
                buttons: {
                    'cancel': {
                        label: action1,
                        className: 'btn-default'
                    },
                    'confirm': {
                        label: action2,
                        className: 'btn-info'
                    }
                },
                callback: function(result) {
                    if (result) {
                        window.location.href = $(that).attr('href');
                    }
                }
            });
        });
        
        /**
         *hide or show form 
         */
        
        $(".hide-show").click(function(){
            var marginRight = parseInt($(".contact-textbox").css('margin-right'));
            if(marginRight >= 0) {
                var width = $(".contact-textbox").outerWidth();
                $(".contact-textbox").css("margin-right","-" + width +"px");
            } else {
                $(".contact-textbox").css("margin-right","0%");
            }
        });
        
        
    });
})(jQuery);

